/************************************************************************/
/*    Author: Scott Bevin                                               */
/************************************************************************/

#include "Messenger.h"
#include "..\XUtilities\Logger.h"

namespace XF
{
	PostOffice *Messenger::msPostOffice = NULL;
	int Messenger::msNextUID = 1;

	Messenger::Messenger()
	{
		mUID = msNextUID;
		msNextUID++;

		if(msPostOffice)
		{
			msPostOffice->RegisterMessenger(this);
		}
	}

	Messenger::~Messenger()
	{
		if(msPostOffice)
			msPostOffice->UnregisterMessenger(this);
	}

	bool Messenger::SubscribeToMailingList( MailingList::ENUM mList )
	{
		if(msPostOffice)
			return msPostOffice->SubscribeToMailingList(mList, this);
		else
			return false;
	}

	void Messenger::UnsubscribeFromMailingList( MailingList::ENUM mList )
	{
		if(msPostOffice)
			msPostOffice->UnsubscribeFromMailingList(mList, this);
	}

	void Messenger::PostAMessage( int receiverID, MessageSubject::ENUM subject, void* data /*= NULL*/, float sendDelay/* = 0*/ )
	{
		if(msPostOffice)
			msPostOffice->PostAMessage(receiverID, mUID, subject, data, sendDelay);
	}

	void Messenger::PostAMessage( MailingList::ENUM mailingList, MessageSubject::ENUM subject, void* data /*= NULL*/, float sendDelay/* = 0*/)
	{
		if(msPostOffice)
			msPostOffice->PostAMessage(mailingList, mUID, subject, data, sendDelay);
	}

	void Messenger::RegisterInPhonebook( std::string name )
	{
		mPhonebookName = name;
		msPostOffice->RegisterInPhonebook(name, ID());
	}

	void Messenger::UnregisterFromPhonebook()
	{
		msPostOffice->UnregisterFromPhonebook(mPhonebookName);
	}

	int Messenger::LookupPhonebook( std::string name )
	{
		return msPostOffice->LookupPhonebook(name);
	}
	bool Messenger::LookupPhonebookSafe( std::string name,int& id )
	{
		return msPostOffice->LookupPhonebookSafe(name,id);
	}
}
